package com.sisyphus.mock.configs;

import androidx.annotation.NonNull;

import com.yanzhenjie.andserver.annotation.Interceptor;
import com.yanzhenjie.andserver.error.HttpException;
import com.yanzhenjie.andserver.framework.HandlerInterceptor;
import com.yanzhenjie.andserver.framework.handler.MethodHandler;
import com.yanzhenjie.andserver.framework.handler.RequestHandler;
import com.yanzhenjie.andserver.framework.mapping.Addition;
import com.yanzhenjie.andserver.http.HttpRequest;
import com.yanzhenjie.andserver.http.HttpResponse;
import com.yanzhenjie.andserver.http.session.Session;

@Interceptor
public class LoginInterceptor implements HandlerInterceptor {

    public static final String LOGIN_ATTRIBUTE = "USER.LOGIN.SIGN";

    @Override
    public boolean onIntercept(
            @NonNull HttpRequest request,
            @NonNull HttpResponse response,
            @NonNull RequestHandler handler) {

        if (handler instanceof MethodHandler) {
            MethodHandler methodHandler = (MethodHandler) handler;
            Addition addition = methodHandler.getAddition();
            if (!isLogin(request, addition)) {
                throw new HttpException(401, "需要登录");
            }
        }

        return false;
    }

    private boolean isLogin(HttpRequest request, Addition addition) {
//        if (isNeedLogin(addition)) {
//            Session session = request.getSession();
//            if (session != null) {
//                Object o = session.getAttribute(LOGIN_ATTRIBUTE);
//                return o instanceof Boolean && (boolean) o;
//            }
//
//            return false;
//        }

        return true;
    }

    private boolean isNeedLogin(Addition addition) {
        if (addition == null) {
            return false;
        }

        String[] stringType = addition.getStringType();
        if (stringType.length == 0) {
            return false;
        }

        boolean[] booleanType = addition.getBooleanType();
        if (booleanType.length == 0) {
            return false;
        }

        return stringType[0].equalsIgnoreCase("login") && booleanType[0];
    }

}
